package ca.jsbr.tinyUI.container 
{
	import ca.jsbr.as2js_lib.display.canvas.View;
	import flash.display.DisplayObject;
	/**
	 * ...
	 * @author jsbr [me@jsbr.ca]
	 */
	public class VBox extends Box
	{
		private var _background:Boolean;
		
		protected var _margin:Array = [10, 10, 10, 10];
		protected var _cellMargin:Array = [0, 0, 0, 0];
		
		public function VBox(background:Boolean=false) 
		{
			_background = background;
			width = 5;
			height = 5;
			draw();
		}
		
		override protected function draw():void 
		{
			if(_background)
				super.draw();
		}
		
		override public function addChild(view:View):void 
		{
			if(view!=_shape)
				addChildBox(view,_childs.length);
			return super.addChild(view);
		}
		
		
		private function addChildBox(child:View,i:int):void 
		{
			//if (child.hasOwnProperty("redraw") && style.autoReDraw)
			//	TinyObject(child).redraw();
			if (_childs.length && _childs[i - 1] != _shape)	{
				child.y = _childs[i - 1].y + _childs[i - 1].height+_cellMargin[0]+_cellMargin[2];
			}
			else {
				child.y = _margin[0];
			}
			child.x = _margin[3];

			height = child.y + child.height + _margin[2]-1;
			if (width < child.width)
				width = child.width + _margin[3]+_margin[1]-1;
		}
		
		override public function redraw(child:Boolean=false):void 
		{
			super.redraw(child);
			
			//for each(var d:View in childs)
			for (var i:int = 0; i < _childs.length; i++ )
			{
				var d:View = _childs[i]; 
				if(d!=_shape)
					addChildBox(d, i);
			}
			
		}
		
		public function setMargin(...arg:Array):void 
		{
			if (arg.length == 4)
				_margin = arg;
			else if (arg.length == 2)
				_margin = [arg[0], arg[1], arg[0], arg[1]];
			else if (arg.length == 1)
				_margin = [arg[0], arg[0], arg[0], arg[0]];
			if(style.autoReDraw)
				redraw();
		}
		
		public function setCellMargin(...arg:Array):void 
		{
			if (arg.length == 4)
				_cellMargin = arg;
			else if (arg.length == 2)
				_cellMargin = [arg[0], arg[1], arg[0], arg[1]];
			else if (arg.length == 1)
				_cellMargin = [arg[0], arg[0], arg[0], arg[0]];
			if(style.autoReDraw)
				redraw();
		}
		
	}

}